package com.edu.robot.core.utils;

import cn.hutool.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 15:34
 **/

@Slf4j
public class HttpClientUtil {

	public static String get(String url, Map<String, Object> params, Map<String, String> headers) {

		Map<String, List<String>> map = getListMap(headers);
		try {
			return HttpRequest.get(url)
					.header(map)
					.form(params)
					.execute(true).body();

		} catch (Exception e) {
		   log.error("http get error", e);
		}

		return null;
	}

	//post请求
	public static String post(String url, Map<String, Object> params, Map<String, String> headers) {
		Map<String, List<String>> map = getListMap(headers);
		try {
			String body = HttpRequest.post(url)
					.header(map)
					.form(params)
					.execute(true).body();
			return body;

		}catch (Exception e){
			log.error("http post error", e);
			return null;
		}
	}

	private static @NotNull Map<String, List<String>> getListMap(Map<String, String> headers) {
		Map<String,List<String>> map =new HashMap<>();

		headers.forEach((k, v)->{
			map.put(k, Collections.singletonList(v));
		});
		return map;
	}
}
